<template>
  <div class="logininput">
    <el-form label-width="70px" :rules="rules" :model="phone" ref="formRef">
      <el-form-item label="手机号" prop="number">
        <el-input v-model="phone.number" />
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <div class="get-code">
          <el-input v-model="phone.code" />
          <el-button> 获取验证码</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { rules } from '../config/phone-config'
import { defineComponent, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { ElForm } from 'element-plus'
import LocalCache from '@/utils/cache'

export default defineComponent({
  setup() {
    const store = useStore()
    const phone = reactive({
      number: LocalCache.getCache('number') ?? '',
      code: LocalCache.getCache('code') ?? ''
    })
    const loginAcction = (ispassword: boolean) => {
      formRef.value?.validate((valid) => {
        if (valid) {
          if (ispassword) {
            LocalCache.setCache('number', phone.number)
          } else {
            LocalCache.deleteCache('number')
          }
          store.dispatch('login/phoneLoginAcction', { ...phone })
        }
      })
    }
    const formRef = ref<InstanceType<typeof ElForm>>()
    return {
      rules,
      formRef,
      phone,
      loginAcction
    }
  }
})
</script>
<style lang="less" scoped>
.get-code {
  display: flex;
  .el-button {
    background-color: rgba(orange, 0.4, 0.5, 0.5);
  }
}
</style>
